From 5ea6ed444411473b7814fe8c88bfb90f98fc629d Mon Sep 17 00:00:00 2001 From: "robertlipe@gmail.com" Date: Thu, 5 Apr 2012 17:02:23 +0000 Subject: [PATCH] Steve adds line styling to TPO3 reader. Hacked KML writer to discard control characters in a description. git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4170 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/kml.c | 13 ++++- gpsbabel/reference/LineStyles.kml | 80 +++++++++++++++++++++++++++++-- gpsbabel/tpo.c | 48 ++++++++++++++----- 3 files changed, 123 insertions(+), 18 deletions(-) diff --git a/gpsbabel/kml.c b/gpsbabel/kml.c index ccd023c09..4d9ee0dbb 100644 --- a/gpsbabel/kml.c +++ b/gpsbabel/kml.c @@ -1473,7 +1473,18 @@ static void kml_waypt_pr(const waypoint* waypointp) xfree(odesc); } else { if (strcmp(waypointp->shortname, waypointp->description)) { - kml_write_xmle("description", waypointp->description); + // This is a hack. The entitizer code really should catch this, + // but this is the low risk fix for now - just toss bad chars. + int j; + char* t = xstrdup(waypointp->description); + for (j = 0; t[j] != 0; j++) { + if (t[j] < ' ') { + t[j] = ' '; + } + } + + kml_write_xmle("description", t); + xfree(t); } } diff --git a/gpsbabel/reference/LineStyles.kml b/gpsbabel/reference/LineStyles.kml index 6753a4c08..86137c815 100644 --- a/gpsbabel/reference/LineStyles.kml +++ b/gpsbabel/reference/LineStyles.kml @@ -187,7 +187,7 @@ NOTE 1 - 12 test lines - see notes with GPX sample!! four line widths for solid red two line widths for dashed red seven colors for medium width Note that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that "red med" is a line label and "Red Medium" is a style label. Waypoints are generated by Topo to exactly match the first track point. Apparently only GPX and KML output from GPSBabel retain track descriptions?? + 12 test lines - see notes with GPX sample!! four line widths for solid red two line widths for dashed red seven colors for medium width Note that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that "red med" is a line label and "Red Medium" is a style label. Waypoints are generated by Topo to exactly match the first track point. Apparently only GPX and KML output from GPSBabel retain track descriptions?? #waypoint -122.277670,37.522060 @@ -195,7 +195,7 @@ NOTE 2 - how GPX from GPSBabel shows Topo track desc as of 2012: (line name and line style are NOT related to actual color or width) <trk> <name>red line</name> <desc>Style=red med, Width=3, Dashed=0, Color=#ff0000</desc> <number>1</number> <trkseg> ... (Topo menu colors are stored in the file as web-style hex RGB) Red=#ff0000, Yellow=#ffff00, Green=#008000, Blue=#000080, Purple=#800080, Black=#000000, White=#ffffff (Topo solid line widths: Hairline=1, Thin=2, Medium=3, Thick=4) (Topo dashed line widths: Hairline=1, Thin=2) SRE's tpo.c mod stuffs Style/Width/Dashed/Color fields into "desc" field under the assumption that something will post-process GPX or KML output and move information from desc to private extensions (solid lines have "Dashed=0", can have width of 1=hairline to 4=thick) (dashed lines have "Dashed=1", can only have width of 1=hairline or 2=thin) + how GPX from GPSBabel shows Topo track desc as of 2012: (line name and line style are NOT related to actual color or width) <trk> <name>red line</name> <desc>Style=red med, Width=3, Dashed=0, Color=#ff0000</desc> <number>1</number> <trkseg> ... (Topo menu colors are stored in the file as web-style hex RGB) Red=#ff0000, Yellow=#ffff00, Green=#008000, Blue=#000080, Purple=#800080, Black=#000000, White=#ffffff (Topo solid line widths: Hairline=1, Thin=2, Medium=3, Thick=4) (Topo dashed line widths: Hairline=1, Thin=2) SRE's tpo.c mod stuffs Style/Width/Dashed/Color fields into "desc" field under the assumption that something will post-process GPX or KML output and move information from desc to private extensions (solid lines have "Dashed=0", can have width of 1=hairline to 4=thick) (dashed lines have "Dashed=1", can only have width of 1=hairline or 2=thin) #waypoint -122.284910,37.522349 @@ -203,7 +203,7 @@ NOTE 3 - how KML from GPSBabel shows Topo track desc as of 2012: (see important notes with GPX sample, all of them apply here!) (note that GPSBabel forces all track styles to be the same - why?) <Folder> <name>Tracks</name> <Folder> <name>red line</name> <snippet/> <description> <![CDATA[<table> <tr><td><b>Description</b> Style=Red Medium, Width=3, Dashed=0, Color=#ff0000 </td></tr> <tr><td><b>Distance</b> 1.3 mi </td></tr> </table>]]> </description> <Folder> <name>Points</name> <Placemark> <name>red line-0</name> ... + how KML from GPSBabel shows Topo track desc as of 2012: (see important notes with GPX sample, all of them apply here!) (note that GPSBabel forces all track styles to be the same - why?) <Folder> <name>Tracks</name> <Folder> <name>red line</name> <snippet/> <description> <![CDATA[<table> <tr><td><b>Description</b> Style=Red Medium, Width=3, Dashed=0, Color=#ff0000 </td></tr> <tr><td><b>Distance</b> 1.3 mi </td></tr> </table>]]> </description> <Folder> <name>Points</name> <Placemark> <name>red line-0</name> ... #waypoint -122.288962,37.522691 @@ -211,7 +211,7 @@ NOTE 4 - how MapSource 6.16.3 adds color to a track as of 2012: (color choices are Dark Gray, Red, Green, Yellow, Blue, Magenta, Cyan, White, Transparent) (should be easy to turn desc tag into their extension, but better to directly write track styles) <trk> <name>Hwy 50 from Placerville</name> <extensions> <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> <gpxx:DisplayColor>Red</gpxx:DisplayColor> </gpxx:TrackExtension> </extensions> <trkseg> ... + how MapSource 6.16.3 adds color to a track as of 2012: (color choices are Dark Gray, Red, Green, Yellow, Blue, Magenta, Cyan, White, Transparent) (should be easy to turn desc tag into their extension, but better to directly write track styles) <trk> <name>Hwy 50 from Placerville</name> <extensions> <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> <gpxx:DisplayColor>Red</gpxx:DisplayColor> </gpxx:TrackExtension> </extensions> <trkseg> ... #waypoint -122.272938,37.522105 @@ -1675,6 +1675,12 @@ Path #lineStyle + 1 @@ -3149,6 +3155,12 @@ Path #lineStyle + 1 @@ -4700,6 +4712,12 @@ Path #lineStyle + 1 @@ -6175,6 +6193,12 @@ Path #lineStyle + 1 @@ -7626,6 +7650,12 @@ Path #lineStyle + 1 @@ -9276,6 +9306,12 @@ Path #lineStyle + 1 @@ -11036,6 +11072,12 @@ Path #lineStyle + 1 @@ -12801,6 +12843,12 @@ Path #lineStyle + 1 @@ -14446,6 +14494,12 @@ Path #lineStyle + 1 @@ -16045,6 +16099,12 @@ Path #lineStyle + 1 @@ -17642,6 +17702,12 @@ Path #lineStyle + 1 @@ -19319,6 +19385,12 @@ Path #lineStyle + 1 diff --git a/gpsbabel/tpo.c b/gpsbabel/tpo.c index 6d0b74687..251fbbfec 100644 --- a/gpsbabel/tpo.c +++ b/gpsbabel/tpo.c @@ -534,6 +534,7 @@ void tpo_process_tracks(void) { unsigned int track_count, track_style_count; unsigned int xx,ii,tmp; +#define TRACKNAMELENGTH 256 int DEBUG=0; @@ -551,11 +552,10 @@ void tpo_process_tracks(void) if (DEBUG) { printf("Unpacking %d track styles...\n",track_style_count); } - char style_name[track_style_count][256]; // some huge value - char style_color[track_style_count][7]; // web color is 6 chars + char style_name[track_style_count][TRACKNAMELENGTH]; // some huge value + int style_color[track_style_count][3]; // keep R/G/B values separate because line_color needs BGR int style_wide[track_style_count],style_dash[track_style_count]; for (ii = 0; ii < track_style_count; ii++) { - style_color[ii][0] = '\0'; // clumsy way to skip two undefined bytes for (xx = 0; xx < 2; xx++) { @@ -564,20 +564,24 @@ void tpo_process_tracks(void) } // next three bytes are RGB color, fourth is unknown + // Topo and web uses rrggbb, also need line_color.bbggrr for KML for (xx = 0; xx < 3; xx++) { - tmp = (unsigned char) gbfgetc(tpo_file_in); - sprintf(style_color[ii], "%s%02x",style_color[ii],tmp); + style_color[ii][xx] = (int) gbfgetc(tpo_file_in); + if((style_color[ii][xx] < 0) || (style_color[ii][xx] >255)) { + style_color[ii][xx] = 0; // assign black if out of range 0x00 to 0xff + // used to store strings: sprintf(style_color[ii], "%s%02x",style_color[ii],tmp); + } } tmp = (unsigned char) gbfgetc(tpo_file_in); - // printf("Skipping unknown byte 0x%x after color=%s\n",tmp,style_color); + // printf("Skipping unknown byte 0x%x after color\n",tmp); // byte for name length, then name tmp = (unsigned char) gbfgetc(tpo_file_in); // wrong byte order?? tmp = tpo_read_int(); // 16 bit value // printf("Track %d has %d-byte (0x%x) name\n",ii,tmp,tmp); - if (tmp >= 256) { - printf("ERROR! Found track style over 128 chars, skipping tracks\n"); + if (tmp >= TRACKNAMELENGTH) { + printf("ERROR! Found track style over TRACKNAMELENGTH chars, skipping all tracks!\n"); return; } if (tmp) { @@ -607,7 +611,7 @@ void tpo_process_tracks(void) } if (DEBUG) { - printf("Track style %d: color=#%s, width=%d, dashed=%d, name=%s\n",ii,style_color[ii],style_wide[ii],style_dash[ii],style_name[ii]); + printf("Track style %d: color=#%02x%02x%02x, width=%d, dashed=%d, name=%s\n",ii,style_color[ii][0],style_color[ii][1],style_color[ii][2],style_wide[ii],style_dash[ii],style_name[ii]); } } @@ -649,7 +653,8 @@ void tpo_process_tracks(void) int lon = 0; unsigned int jj; route_head* track_temp; - + char rgb[7],bgr[7]; + int bbggrr = 0; // Allocate the track struct track_temp = route_head_alloc(); @@ -662,7 +667,7 @@ void tpo_process_tracks(void) track_style = tpo_read_int(); // index into freehand route styles defined in this .tpo file track_style -= 1; // STARTS AT 1, whereas style arrays start at 0 - // Can be 8/16/32-bit value + // Can be 8/16/32-bit value - never used? track_length = tpo_read_int(); //UNKNOWN DATA LENGTH @@ -677,13 +682,30 @@ void tpo_process_tracks(void) xasprintf(&track_name, "TRK %d", ii+1); } track_temp->rte_name = track_name; + + // RGB line_color expressed for html=rrggbb and kml=bbggrr - not assigned before 2012 + sprintf(rgb,"%02x%02x%02x",style_color[track_style][0],style_color[track_style][1],style_color[track_style][2]); + sprintf(bgr,"%02x%02x%02x",style_color[track_style][2],style_color[track_style][1],style_color[track_style][0]); + sscanf(bgr,"%06x",&bbggrr); // hex string to integer - probably not the best way to do style_color to bbggrr + track_temp->line_color.bbggrr = bbggrr; + + // track texture (dashed=1, solid=0) mapped into opacity - not assigned before 2012 + track_temp->line_color.opacity = 0xff; // 255 + if(style_dash[track_style]) { + track_temp->line_color.opacity = 0x50; + } + + // track width, from 1=hairline to 4=thick in Topo - not assigned before 2012 + // (what are correct values for KML or other outputs??) + track_temp->line_width = style_wide[track_style]; + if (DEBUG) printf("Track Name: %s, ?Type?: %d, Style Name: %s, Width: %d, Dashed: %d, Color: #%s\n", - track_name, line_type, style_name[track_style], style_wide[track_style], style_dash[track_style], style_color[track_style]); + track_name, line_type, style_name[track_style], style_wide[track_style], style_dash[track_style],rgb); // Track description // track_temp->rte_desc = NULL; // pre-2012 default, next line from SRE saves track style as track description xasprintf(&track_temp->rte_desc, "Style=%s, Width=%d, Dashed=%d, Color=#%s", - style_name[track_style], style_wide[track_style], style_dash[track_style], style_color[track_style]); + style_name[track_style], style_wide[track_style], style_dash[track_style], rgb); // Route number track_temp->rte_num = ii+1; -- 2.30.2